package com.diy.sigmund.diyuser.test.controller;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.util.json.JacksonUtil;
import com.diy.sigmund.diyuser.test.entity.RestTemplateTemp;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * @author ylm-sigmund
 * @since 2021/8/17 11:26
 */
@RequestMapping("/restTemplate")
@RestController()
public class RestTemplateController {

    @Resource
    private RestTemplate restTemplate;

    /////////////////////////GET请求

    @GetMapping("/getUser")
    public ResultMsg getUser(RestTemplateTemp form) {
        int a =1/0;
        RestTemplateTemp restTemplateTemp = new RestTemplateTemp();
        restTemplateTemp.setUserName(form.getUserName());
        restTemplateTemp.setAge(form.getAge());
        return ResultMsg.success(restTemplateTemp);
    }

    /**
     * 普通访问
     */
    @PostMapping("/getForObject")
    public ResultMsg getForObject() {

        ResultMsg result = restTemplate
                .getForObject("http://localhost:8002/restTemplate/getUser?userName=张三&age=18", ResultMsg.class);
                // .getForObject("http://localhost:8002/restTemplate/getUser", ResultMsg.class);
        // java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.diy.sigmund.diyuser.entity.TempUser
        // TempUser tempUser = (TempUser) result.getData();
        // 使用序列化解决
        final RestTemplateTemp restTemplateTemp = JacksonUtil.toObject(JacksonUtil.toJson(result.getData()), RestTemplateTemp.class);

        return result;
    }

    /**
     * 返回HTTP状态
     */
    @PostMapping("/getForEntity")
    public ResultMsg getForEntity() {
        ResponseEntity<ResultMsg> responseEntity = restTemplate
                .getForEntity("http://localhost:8080/restTemplate/getUser?userName=张三&age=18", ResultMsg.class);
        // 获取状态对象
        HttpStatus httpStatus = responseEntity.getStatusCode();
        // 获取状态码
        int statusCodeValue = responseEntity.getStatusCodeValue();
        // 获取headers
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        // 获取body
        ResultMsg result = responseEntity.getBody();
        return result;
    }

    /**
     * 映射请求参数
     */
    @PostMapping("/getForObjectByMapping")
    public ResultMsg getForObjectByMapping() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userName", "张三");
        paramMap.put("age", 18);
        ResultMsg result = restTemplate
                .getForObject("http://localhost:8080/restTemplate/getUser?userName={userName}&age={age}",
                        ResultMsg.class, paramMap);
        return result;
    }

    /////////////////////////POST请求

    @PostMapping("/getPostUser")
    public RestTemplateTemp getPostUser(@RequestBody RestTemplateTemp form) {
        RestTemplateTemp restTemplateTemp = new RestTemplateTemp();
        restTemplateTemp.setUserName(form.getUserName());
        restTemplateTemp.setAge(form.getAge());
        return restTemplateTemp;
    }

    @PostMapping("/postForObject")
    public RestTemplateTemp postForObject() {
        RestTemplateTemp param = new RestTemplateTemp();
        param.setUserName("张三");
        param.setAge(18);
        RestTemplateTemp result = restTemplate
                .postForObject("http://localhost:8080/restTemplate/getPostUser", param, RestTemplateTemp.class);
        return result;
    }

    @PostMapping("/postForObjectAndHead")
    public RestTemplateTemp postForObjectAndHead() {
        // 请求头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
        //headers.add("headParam1", "headParamValue");

        // 请求体内容
        RestTemplateTemp param = new RestTemplateTemp();
        param.setUserName("张三");
        param.setAge(18);

        // 组装请求信息
        HttpEntity<RestTemplateTemp> httpEntity = new HttpEntity<>(param, headers);

        RestTemplateTemp result = restTemplate
                .postForObject("http://localhost:8080/restTemplate/getPostUser", httpEntity, RestTemplateTemp.class);
        return result;
    }

    ///////////////////////无请求体的访问

    /**
     * 仅method为post，传参方式仍然为get的param方式
     */
    @PostMapping("/getPostUserNoBody")
    public RestTemplateTemp getPostUserNoBody(RestTemplateTemp form) {
        RestTemplateTemp restTemplateTemp = new RestTemplateTemp();
        restTemplateTemp.setUserName(form.getUserName());
        restTemplateTemp.setAge(form.getAge());
        return restTemplateTemp;
    }

    @PostMapping("/postForObjectNoBody")
    public RestTemplateTemp postForObjectNoBody() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userName", "张三");
        paramMap.put("age", 18);
        RestTemplateTemp result = restTemplate
                .postForObject("http://localhost:8080/restTemplate/getPostUserNoBody?userName={userName}&age={age}",
                        null, RestTemplateTemp.class, paramMap);
        return result;
    }

    /////////////////上传文件

    @PostMapping("/uploadFile")
    public RestTemplateTemp uploadFile(HttpServletRequest request, RestTemplateTemp form) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        //获取文件信息
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        RestTemplateTemp restTemplateTemp = new RestTemplateTemp();
        if (multipartFile != null) {
            restTemplateTemp.setUserName(form.getUserName() + " " + multipartFile.getOriginalFilename());
        }
        if (form != null) {
            restTemplateTemp.setAge(form.getAge());
        }
        return restTemplateTemp;
    }

    @PostMapping("/postForObjectFile")
    public RestTemplateTemp postForObjectFile() {
        // 文件
        FileSystemResource file = new FileSystemResource("C:\\Users\\ylm\\Desktop\\51CTO下载-Redis命令参考手册完整版.pdf");

        // 设置请求内容
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", file);
        // 其他参数
        param.add("userName", "张三");
        param.add("age", 18);

        // 组装请求信息
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param);

        // 发送请求
        RestTemplateTemp result = restTemplate
                .postForObject("http://localhost:8080/restTemplate/uploadFile", httpEntity, RestTemplateTemp.class);
        return result;
    }
}
